package cn.itcast.search.controller;

import cn.itcast.feign.client.ItemClient;
import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.dto.search.SearchReqDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.hmall.pojo.item.ItemDoc;
import cn.itcast.search.service.SearchService;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/search")
@RestController
@Slf4j
public class SearchController {
    @Autowired
    private RestHighLevelClient client;
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private SearchService searchService;

    /**
     * 全量数据导入
     */
    @GetMapping("/importItemData")
    public ResultDTO importItemData() {
        try {
            int page = 1;
            int size = 1000;
            while (true) {
                BulkRequest hotel = new BulkRequest("item");
                HashMap<String, Object> map = new HashMap<>();
                map.put("page", page);
                map.put("size", size);
                PageDTO<Item> list1 = itemClient.list(map);
                List<Item> list = list1.getList();
                if (list.size() > 0) {
                    for (Item item : list) {
                        IndexRequest item1 = new IndexRequest("item").id(item.getId().toString());
                        ItemDoc itemDoc = new ItemDoc(item);
                        item1.source(JSON.toJSONString(itemDoc), XContentType.JSON);
                        hotel.add(item1);
                    }
                    BulkResponse bulk = client.bulk(hotel, RequestOptions.DEFAULT);
                    System.out.println(bulk.status());
                } else {
                    System.out.println("结束");
                    System.out.println(page);
                    break;
                }
                page++;
            }
            return ResultDTO.ok();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("批量导入es索引库数据失败   原因: {}", e.getMessage());
            return ResultDTO.error("批量导入es索引库数据失败");
        }
    }


    /**
     * 搜索栏自动补全功能
     */
    @GetMapping("/suggestion")
    public List<String> suggestion(@RequestParam String key) {
        return searchService.suggestion(key);
    }

    /**
     * 过滤项聚合功能
     */
    @PostMapping("/filters")
    public Map<String,List<String>> filter(@RequestBody SearchReqDTO searchReqDTO) {
        return searchService.getFilter(searchReqDTO);
    }

    /**
     * 基本搜索功能
     */
    @PostMapping("/list")
    public PageDTO<ItemDoc> list(@RequestBody SearchReqDTO searchReqDTO) {
        return searchService.getItemList(searchReqDTO);
    }






}
